package cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.base;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.ValidateException;
import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.ValidateUtils;

public class SecureApiRequest extends BaseApiRequest {

    private Long accountId;

    /**
     * 签名
     */
    private String sign;

    public SecureApiRequest() {
    }

    /**
     * @param timestamp
     * @param uuid
     * @param accountId
     */
    public SecureApiRequest(Long timestamp, String uuid, Long accountId) {
        this.timestamp = timestamp;
        this.uuid = uuid;
        this.accountId = accountId;
    }

    /**
     * @param timestamp
     * @param uuid
     * @param accountId
     * @param secretKey
     */
    public SecureApiRequest(Long timestamp, String uuid, Long accountId, String secretKey) {
        this.timestamp = timestamp;
        this.uuid = uuid;
        this.accountId = accountId;
        this.sign = this.getSign(secretKey);
    }

    /**
     * @param timestamp
     * @param uuid
     * @param version
     * @param accountId
     * @param secretKey
     */
    public SecureApiRequest(Long timestamp, String uuid, String version, Long accountId, String secretKey) {
        this.timestamp = timestamp;
        this.uuid = uuid;
        this.version = version;
        this.accountId = accountId;
        this.sign = this.getSign(secretKey);
    }

    @Override
    public boolean validate() throws ValidateException {
        try {
            return ValidateUtils.validate(this);
        } catch (ValidateException e) {
            throw new ValidateException(e.getMessage());
        }
    }

    @Override
    public boolean validateOnce() throws ValidateException {
        try {
            return ValidateUtils.validateOnce(this);
        } catch (ValidateException e) {
            throw new ValidateException(e.getMessage());
        }
    }

    /**
     * 获取签名
     *
     * @param secretKey
     * @return
     */
    public String getSign(String secretKey) {
        StringBuilder sb = new StringBuilder();
        sb.append(this.accountId);
        sb.append(this.timestamp);
        sb.append(this.uuid);
        sb.append(this.version);
        HMac hMac = SecureUtil.hmac(HmacAlgorithm.HmacSHA256, secretKey);
        return hMac.digestHex(sb.toString());
    }

    public String getSign(SecureApiRequest secureApiRequest, String secretKey) {
        StringBuilder sb = new StringBuilder();
        sb.append(secureApiRequest.getAccountId());
        sb.append(secureApiRequest.getTimestamp());
        sb.append(secureApiRequest.getUuid());
        sb.append(secureApiRequest.getVersion());
        HMac hMac = SecureUtil.hmac(HmacAlgorithm.HmacSHA256, secretKey);
        return hMac.digestHex(sb.toString());
    }

    public Long getAccountId() {
        return accountId;
    }

    public void setAccountId(Long accountId) {
        this.accountId = accountId;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }
}

